package generovanieRozpisu;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import strukturaRozpis.Rozpis;

public class SpustacGeneratora {

    private Rozpis r;
    private Connection con = null;
    private String url = "jdbc:mysql://kempelen.ii.fmph.uniba.sk:3306/";
    private String db = "vratnica";
    private String driver = "com.mysql.jdbc.Driver";
    private String user = "vratnica";
    private String pass = "MonikaVeronika";
    int year, month;

    public SpustacGeneratora(int mesiac, int rok) throws SQLException, ParseException {
        year = rok;
        month = mesiac;

        if (connectAndTry() == false) {
            //r = new Generator("01-" + String.format("%02d", mesiac) + "-" + String.format("%02d", rok)).getRozpis();
            Rozpis lastMonth;
            if (mesiac == 1) {
                lastMonth = new Rozpis(12, rok - 1);
            } else {
                lastMonth = new Rozpis(mesiac - 1, rok);
            }

            ondrejovGenerator g = new ondrejovGenerator(mesiac, rok, lastMonth.getPoslednuSluzbuMinulehoMesiaca());
            //nebere do uvahy poslednu sluzbu minuleho lebo sa nevola comit sluzby po kazdom vygenerovani (stale si mysli ze minuly mesiac neboli zmeny)

            r = g.generuj();

            //r.commitSluzby();
        } else {
            r = new Rozpis(mesiac, rok);
        }

    }

    public Rozpis getRozpis() {
        return r;
    }

    private boolean connectAndTry() {
        try {
            Class.forName(driver).newInstance();
            con = DriverManager.getConnection(url + db, user, pass);
            Statement st = con.createStatement();

            //pocet dni v danom mesiaci a roku
            Calendar c = new GregorianCalendar(year, month - 1, 1);
            int pocetDni = c.getActualMaximum(Calendar.DAY_OF_MONTH);

            //kontrola ci sa v tabulke uz nachadza vygenerovany rozpis
            String fromDate = year + "-" + month + "-1 00:00:00";
            String toDate = year + "-" + month + "-" + pocetDni + " 23:59:59";
            ResultSet resCount = st.executeQuery("SELECT count(*) FROM zmeny_table WHERE datum_cas BETWEEN '" + fromDate + "' AND '" + toDate + "'");
            System.out.println("SELECT count(*) FROM zmeny_table WHERE datum_cas BETWEEN '" + fromDate + "' AND '" + toDate + "'");
            while (resCount.next()) {
                System.out.println(resCount.getInt("count(*)"));
                if (resCount.getInt("count(*)") == 0) {
                    System.out.println("koniec");
                    return false;
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(SpustacGeneratora.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }
}
